/**
 * @author JackGao
 *			email:jackgao.china@gmail.com
 *  date：2016-6-24
 * 
 */
package cn.cuit.ds.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.springframework.stereotype.Repository;

import cn.cuit.ds.dao.IOrderDetailDao;
import cn.cuit.ds.domain.OrderDeatil;
import cn.cuit.ds.utils.JdbcUtils;

@Repository(value = "OrderDetailDao")
public class OrderDetailDao implements IOrderDetailDao {

	//
	public List<OrderDeatil> getAllDetailByShopperId(String shopperId) {
		QueryRunner runner = JdbcUtils.getQueryRunner();
		String sql = "SELECT orders.ordersid,orders.orderTime,address.address,address.realName,address.phone,orders.detail2,orders.orderName,orders.detail1 FROM orders INNER JOIN address ON orders.addressId=address.addressId WHERE orders.shoperId= ? ORDER BY orders.orderTime";
		try {
			return runner.query(sql, new BeanListHandler<OrderDeatil>(
					OrderDeatil.class), shopperId);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}

	public List<OrderDeatil> getAllDetailByuserId(String userId) {
		//
		QueryRunner runner = JdbcUtils.getQueryRunner();
		String sql = "SELECT orders.ordersid,orders.orderTime,"
				+ "address.address,address.realName,address.phone,"
				+ "orders.detail2,orders.orderName, orders.detail1"
				+ " FROM orders " + "INNER JOIN address"
				+ " ON orders.addressId=address.addressId"
				+ " WHERE address.userId=? " + "ORDER BY orders.orderTime";
		try {
			return runner.query(sql, new BeanListHandler<OrderDeatil>(
					OrderDeatil.class), userId);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}

	public void changeState(String ordersId) {
		// TODO Auto-generated method stub
		QueryRunner runner = JdbcUtils.getQueryRunner();
		String sql = "update orders set detail1='审核通过' where ordersId=?";
		try {
			runner.update(sql, ordersId);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public List<OrderDeatil> getDetailByUserIdAndTime(String userId,
			String timeFrom, String timeTo) {
		QueryRunner runner = JdbcUtils.getQueryRunner();
		String sql = "SELECT orders.ordersid,"
				+ "orders.orderTime,address.address,address.realName,"
				+ "address.phone,orders.detail2,orders.orderName, "
				+ "orders.detail1 FROM orders INNER JOIN address "
				+ "ON orders.addressId=address.addressId "
				+ "WHERE address.userId=?  "
				+ "AND (orderTime BETWEEN ? AND ? )"
				+ "ORDER BY orders.orderTime";
		try {
			return runner.query(sql, new BeanListHandler<OrderDeatil>(
					OrderDeatil.class), userId,timeFrom,timeTo);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}

}
